﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Z2.Core
{

	/// <summary>
	/// Worlds background
	/// </summary>
	public class Background : IDraw
	{

		#region Const

		private const string Group = "Background";

		#endregion

		#region Members

		private Sprite m_sprite;
		private bool m_static;
		private bool m_isDay;
		private int m_index;
		private Vector2 m_location;
		private Vector2 m_location2;

		private float m_slowFactor;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="asset">Sprite asset</param>
		public Background(string asset)
		{
			this.m_sprite = SpriteManager.Instance.LoadSpriteCache(asset, 1, 1, Background.Group);
			
			this.m_sprite.Depth = Depths.Background + (RandomGenerator.Random.Next(0, 1000) / 100000F);
			
			this.m_location = Vector2.Zero;
			this.m_location2 = new Vector2(this.m_sprite.FrameWidth, 0);

			this.IsStatic = true;
			this.m_slowFactor = 1;
		}

		#endregion

		#region Properties

		public Sprite Sprite
		{
			get { return this.m_sprite; }
		}

		public bool IsDay
		{
			get { return this.m_isDay; }
			set { this.m_isDay = value; }
		}

		/// <summary>
		/// Gets or sets slowfactor
		/// </summary>
		public float SlowFactor
		{
			get { return this.m_slowFactor; }
			set { this.m_slowFactor = value; }
		}

		/// <summary>
		/// Gets or sets background index (for main background set 0)
		/// </summary>
		public int Index
		{
			get { return this.m_index; }
			set
			{ 
				this.m_index = value;
				this.m_sprite.Depth = Depths.Background + (Convert.ToSingle(value) / 10.0f);
			}
		}

		/// <summary>
		/// Indicates whether this background is static or not
		/// </summary>
		public bool IsStatic
		{
			get { return this.m_static; }
			set { this.m_static = value; }
		}
		
		/// <summary>
		/// Gets camera X position
		/// </summary>
		private float CameraX
		{
			get { return Camera.Instance.Position.X / this.m_slowFactor; }
		}

		#endregion

		#region Members

		/// <summary>
		/// Updates background position
		/// </summary>
		public void Update()
		{
			if (this.IsDay) this.Sprite.Visibility = World.Current.GlobalLight;
			if (this.IsStatic) return;

			if (this.m_location.X + this.m_sprite.FrameWidth + this.CameraX <= 0) this.m_location.X += (2 * this.m_sprite.FrameWidth);
			if (this.m_location.X + this.CameraX >= this.m_sprite.FrameWidth) this.m_location.X -= (2 * this.m_sprite.FrameWidth);

			if (this.m_location2.X + this.m_sprite.FrameWidth + this.CameraX <= 0) this.m_location2.X += (2 * this.m_sprite.FrameWidth);
			if (this.m_location2.X + this.CameraX >= this.m_sprite.FrameWidth) this.m_location2.X -= (2 * this.m_sprite.FrameWidth);
		}

		/// <summary>
		/// Draws background
		/// </summary>
		/// <param name="batch"><see cref="SpriteBatch"/></param>
		public void Draw(SpriteBatch batch)
		{
			if (this.IsStatic)
			{
				this.m_sprite.Draw(batch, Vector2.Zero);
				return;
			}
			Vector2 p = new Vector2(this.m_location.X + this.CameraX, 0);
			Vector2 p2 = new Vector2(this.m_location2.X + this.CameraX, 0);
			this.m_sprite.Draw(batch, p);
			this.m_sprite.Draw(batch, p2);
		}

		public void DrawLight(SpriteBatch batch)
		{
		}

		#endregion
	}
}
